//
//  SelectTypeView.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/21.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "SelectTypeView.h"
#import "SelectTypeCell.h"
@interface SelectTypeView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *bgBlackView;
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UIView *selectView;
@end

static NSString *typeCell = @"typeCell";

@implementation SelectTypeView

- (instancetype)initWithDataSource:(NSMutableArray *)dataArray{
    if (self = [super init]) {
        self.dataArray = dataArray;
        [self loadSubviews];
    }
    return self;
}


- (void)loadSubviews{
    // window
    self.backgroundColor = [UIColor clearColor];
    self.window = [UIApplication sharedApplication].keyWindow;
    [self.window addSubview:self.bgBlackView];
    UIView *selectView = [[UIView alloc]init];
    self.selectView = selectView;
    selectView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:selectView];
    [selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.window);
        make.height.mas_equalTo(250*HEIGHT);
    }];
    // 提示
    UIView *topTintView = [[UIView alloc]init];
    topTintView.backgroundColor = KLColor_A8;
    [selectView addSubview:topTintView];
    [topTintView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(selectView);
        make.height.mas_equalTo(40*HEIGHT);
    }];
    
    
    UILabel *tintLb = [[UILabel alloc]init];
    tintLb.text = @"选择分类";
    tintLb.textColor = KLColor_B12;
    tintLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15*WIDTH];
    [topTintView addSubview:tintLb];
    [tintLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topTintView);
        make.left.equalTo(topTintView).offset(15*WIDTH);
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setBackgroundImage:[UIImage imageNamed:@"icon_fenlei_close"] forState:UIControlStateNormal];
    [topTintView addSubview:cancelBtn];
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self cancel];
    }];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topTintView).offset(-10*WIDTH);
        make.centerY.equalTo(topTintView);
        make.width.height.mas_equalTo(34*WIDTH);
    }];
    
    // 下面的标签内容
    [self.selectView addSubview:self.collectionView];
   
    
    
    
    
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置collectionView的滚动方向，需要注意的是如果使用了collectionview的headerview或者footerview的话， 如果设置了水平滚动方向的话，那么就只有宽度起作用了了
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        [layout setMinimumLineSpacing:12*HEIGHT];
        [layout setMinimumInteritemSpacing:12*WIDTH];
        [layout setItemSize:CGSizeMake(76*WIDTH, 32*HEIGHT)];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15*WIDTH, 55*HEIGHT, 340*WIDTH,164*HEIGHT) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[SelectTypeCell class] forCellWithReuseIdentifier:typeCell];
        //注册头视图
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = YES;
    }
    return _collectionView;
}

#pragma mark -- UICollectionViewDataSource
/** 每组cell的个数*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

/** cell的内容*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SelectTypeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:typeCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    [cell.titleBtn setTitle:self.dataArray[indexPath.row] forState:UIControlStateNormal];
    return cell;
}




#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击了第 %zd组 第%zd个",indexPath.section, indexPath.row);
    SelectTypeCell *cell = (SelectTypeCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.titleBtn.backgroundColor = KLColor_A1;
    [cell.titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if (self.typeDelegate) {
        [self.typeDelegate sendNext:@{@"index":@(indexPath.row)}];
    }
    [self cancel];
    
}


- (void)cancel{
    [self.bgBlackView removeFromSuperview];
    [self.selectView removeFromSuperview];
    [self removeFromSuperview];

}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (UIView *)bgBlackView{
    if (!_bgBlackView) {
        _bgBlackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, KLScreenH)];
        _bgBlackView.backgroundColor = KLColor(0, 0, 0, 0.5);
    }
    return _bgBlackView;
}

@end
